package app.handler;

import app.vo.ResultResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class NoRightAccessHandler implements AccessDeniedHandler {
    @Autowired
    private ResultResponse resultResponse;

    //访问拒绝处理程序
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        resultResponse.setCode(403);
        resultResponse.setMessage("您无权访问");
        resultResponse.setData(e.getMessage());
        ResultResponse resultResponse = new ResultResponse(this.resultResponse.getCode(), this.resultResponse.getMessage(),this.resultResponse.getState(), this.resultResponse.getData());
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(resultResponse));
    }
}
